package net.slidinglive.utilities.util;

import java.util.ArrayList;
import java.util.List;

import net.slidinglive.utilities.model.Address;
import net.slidinglive.utilities.model.Contact;
import net.slidinglive.utilities.model.EmailAddress;
import net.slidinglive.utilities.model.PhoneNumber;
import android.app.Activity;
import android.database.Cursor;
import android.net.Uri;
import android.provider.ContactsContract;
import android.support.v4.content.CursorLoader;

/**
 * @author Sebastian Hoffmann <bo.hoffi@gmail.com>
 */
public class ContactHelper {

    public static List<Contact> getExistingContacts(final Activity context) {

        final List<Contact> contacts = new ArrayList<Contact>();

        final Uri uri = ContactsContract.Contacts.CONTENT_URI;

        final CursorLoader loader =
                new CursorLoader(context, uri, null, null, null, ContactsContract.Contacts.DISPLAY_NAME + " ASC");
        final Cursor contactCursor = loader.loadInBackground();

        if (contactCursor.moveToFirst()) {
            do {
                final Contact c = new Contact(contactCursor);

                for (final PhoneNumber number : getPhoneNumbers(context, contactCursor, c.getId().toString())) {
                    c.addPhoneNumber(number);
                }

                for (final EmailAddress emailaddress : getEmailAddresses(context, c.getId().toString())) {
                    c.addEmailAddress(emailaddress);
                }

                for (final Address address : getAddresses(context, c.getId().toString())) {
                    c.addAddress(address);
                }

                if (c.getPhoneNumbers().size() > 0) {

                    contacts.add(c);
                }

            } while (contactCursor.moveToNext());
        }

        contactCursor.close();

        return contacts;
    }

    private static List<PhoneNumber> getPhoneNumbers(final Activity context, final Cursor contactCursor,
            final String contactId) {

        final List<PhoneNumber> numbers = new ArrayList<PhoneNumber>();

        if (Integer.parseInt(contactCursor.getString(contactCursor
                .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {

            final CursorLoader loader =
                    new CursorLoader(context, ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                            ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] { contactId },
                            null);
            final Cursor phoneNumberCursor = loader.loadInBackground();

            while (phoneNumberCursor.moveToNext()) {

                final PhoneNumber number = new PhoneNumber(phoneNumberCursor);
                numbers.add(number);
            }
            phoneNumberCursor.close();
        }

        return numbers;
    }

    private static List<EmailAddress> getEmailAddresses(final Activity context, final String contactId) {

        final List<EmailAddress> emailAddresses = new ArrayList<EmailAddress>();

        final CursorLoader loader =
                new CursorLoader(context, ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,
                        ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?", new String[] { contactId }, null);
        final Cursor emailAddressCursor = loader.loadInBackground();

        while (emailAddressCursor.moveToNext()) {

            final EmailAddress emailAddress = new EmailAddress(emailAddressCursor);
            emailAddresses.add(emailAddress);
        }
        emailAddressCursor.close();

        return emailAddresses;
    }

    private static List<Address> getAddresses(final Activity context, final String contactId) {

        final List<Address> addresses = new ArrayList<Address>();

        final String addrWhere =
                ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?";
        final String[] addrWhereParams =
                new String[] { contactId, ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE };

        final CursorLoader loader =
                new CursorLoader(context, ContactsContract.Data.CONTENT_URI, null, addrWhere, addrWhereParams, null);
        final Cursor addressCursor = loader.loadInBackground();

        while (addressCursor.moveToNext()) {

            final Address address = new Address(addressCursor);
            addresses.add(address);
        }
        addressCursor.close();

        return addresses;
    }
}